#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2020/12/11 10:10
    Desc  :
--------------------------------------
"""

import json

from flask import Blueprint, render_template, request

from application import app
from common.libs.JsonResponseUtils import JsonResponse
from common.libs.paginationUtils import PaginationUtils
from common.libs.queryListUtils import QueryListUtils
from servers.models.logModel import Log

log_route = Blueprint('log', __name__)


# 日志列表
@log_route.route('/list', methods = ['POST'])
def logList():

    try:
        form = {} if not request.json else dict(request.json)
        page, size = form.get('page'), form.get('size')
        form = QueryListUtils(form).filter_by('icontains')
        pagination = PaginationUtils(Log.objects(**form).order_by('-id'), page = page, size = size)
        return JsonResponse(code = 20000, data = pagination.items, message = '请求成功').jsonExit()
    except Exception as e:
        app.logger.error(e)
        return JsonResponse(code = 50000, data = None, message = "系统异常", errors = f'{e}').jsonExit()


# 用户详情
@log_route.route('/info', methods = ['POST'])
def userInfo():
    try:
        form = {} if not request.json else dict(request.json)
        if form.get("id") is None or form.get("id") == "":
            return JsonResponse(code = 40002, data = None, message = "id不能为空").jsonExit()

        log = Log.objects(id = form.get('id'))

        return JsonResponse(code = 20000, data = log.first().to_json(), message = "请求成功").jsonExit()

    except Exception as e:
        app.logger.error(e)
        return JsonResponse(code = 50000, data = None, message = "系统异常", errors = f'{e}').jsonExit()